# -*- coding: utf-8 -*-

import os
import os.path
import sys
import ConfigParser
import wx

CONFIGFILE = '.Mayapad'
configDir = ''
if sys.platform == "win32":
	configDir = os.environ[ 'USERPROFILE' ]
else:
	configDir = os.environ['HOME']
if len(configDir):
	CONFIGFILE = configDir + os.sep + CONFIGFILE


def saveOption(name, val):
	config = ConfigParser.ConfigParser()
	if os.path.exists(CONFIGFILE):
		config.read(CONFIGFILE)
	if not config.has_section('OPTIONS'):
		config.add_section('OPTIONS')
	if isinstance(val, str):
		#print 'this is str'
		formattedValue = val
	elif isinstance(val, bool):
		#print 'this is bool'
		# have to convert to 1,0 from True,False
		formattedValue = str(int(val))
	elif isinstance(val, int):
		#print 'this is int'
		formattedValue = str(val)
	#elif isinstance(val, unicode):
	#	#print 'this is unicode'
	#	formattedValue = val.encode('mbcs')
	elif isinstance(val, list):
		formattedValue = ''
		for v in val:
			if isinstance(v, bool):
				v = str(int(v))
				formattedValue += '[[[' + str(v) + ']]],'
			elif isinstance(v, unicode):
				unicodeRes = u'[[[' + v + u']]],'
				if sys.platform == "win32":
					formattedValue += unicodeRes.encode('mbcs')
				else:
					formattedValue += unicodeRes.encode('utf_8')
			else:
				formattedValue += '[[[' + str(v) + ']]],'
	elif isinstance(val, wx._gdi.Colour):
		# colour
		formattedValue = '#' +'%02x%02x%02x' % (val[0], val[1], val[2])
	else:
		print type(val)
		formattedValue = str(val)
		
	config.set('OPTIONS', name, formattedValue)
	# save to file
	o=open(CONFIGFILE,'w')
	config.write(o)
	o.close()


def getOption(name):
	if not os.path.exists(CONFIGFILE):
		return None
	
	config = ConfigParser.ConfigParser()
	config.read(CONFIGFILE)
	if not config.has_section('OPTIONS'):
		return None
	
	if config.has_option('OPTIONS', name):
		return config.get('OPTIONS', name)
	else:
		return None


def getListOption(name):
	if not os.path.exists(CONFIGFILE):
		return None
	
	config = ConfigParser.ConfigParser()
	config.read(CONFIGFILE)
	if not config.has_section('OPTIONS'):
		return None
	
	if config.has_option('OPTIONS', name):
		rawValue = config.get('OPTIONS', name)
		rawSplitted = rawValue[:-1].split(',')
		res = []
		for s in rawSplitted:
			dataStr = s[3:-3]
			if dataStr == 'True':
				res.append(True)
			elif dataStr == 'False':
				res.append(False)
			elif dataStr.isdigit():
				res.append(int(dataStr))
			else:
				res.append(dataStr)
		return res
	else:
		return None


def hasOption(name):
	if not os.path.exists(CONFIGFILE):
		return None
	
	config = ConfigParser.ConfigParser()
	config.read(CONFIGFILE)
	if not config.has_section('OPTIONS'):
		return None
	
	if config.has_option('OPTIONS', name):
		return True
	else:
		return None


def removeOption(name):
	if not os.path.exists(CONFIGFILE):
		return None
	
	config = ConfigParser.ConfigParser()
	config.read(CONFIGFILE)
	if not config.has_section('OPTIONS'):
		return None
	
	if config.has_option('OPTIONS', name):
		config.remove_option('OPTIONS', name)
		# save to file
		o=open(CONFIGFILE,'w')
		config.write(o)
		o.close()
		return True
	else:
		return None
